<?php

error_reporting(E_ALL|E_STRICT); // return all possible errors:
//error_reporting(E_PARSE); // return only parsing errors:
ini_set('display_errors', 1); //disable on production servers!

// set this to false if you use a cron job for housekeeping:
define("DOHOUSEKEEPING", true);

// set the default timezone:
date_default_timezone_set('Europe/Berlin');

// needed to load all the other classes:
require_once('library.php');

// load messages after this id:
$after = intval(LEChat::getParameter('after'));

// get the token from either POST, GET or COOKIE:
$token = LEChat::getParameter('token', true);

// create the chat instance:
$lc = LEChat::getInstance();

// make sure there is a token:
$token = $lc->initUser($token, $after);

// set the token as a cookie 
setcookie('token', $token);

// request to change the user name?
if (LEChat::hasParameter('setname')) {
	$newUserName = LEChat::getParameter('setname');
	$lc->changeUserName($token, $newUserName);
}

// request to post a message?
if (LEChat::hasParameter('msg')) {
	$newMessage = LEChat::getParameter('msg');
	$replyTo = LEChat::getParameter('replyTo');
	$lc->postMessage($token, $newMessage, $replyTo);
}

// shall I do housekeeping in the request?
if (DOHOUSEKEEPING) {

	// update all user status:
	$lc->usersHousekeeping();

	// remove expired messages
	$lc->messagesHousekeeping();
}

// print a user list (all users if it's the first request)
$lc->printUsers($after==0 ? true : false);

// show a list of all pending messages:
$lc->printMessages($after);

